﻿using DataCollection.Core.Models;
using DataCollection.Infrastructure;
using CommunityToolkit.Mvvm.ComponentModel;
using Mapster;

namespace DataCollection.WinForm.ViewModel
{
    internal partial class DevicePointViewModel : ObservableObject
    {
        #region 属性
        private readonly DevicePointModel model = new DevicePointModel();

        [ObservableProperty]
        private long number;

        [ObservableProperty]
        private string name = string.Empty;

        [ObservableProperty]
        private string address = string.Empty;

        [ObservableProperty]
        private string pointType = string.Empty;

        [ObservableProperty]
        private double rangeMax;

        [ObservableProperty]
        private double rangeMin;

        [ObservableProperty]
        private double outMax;

        [ObservableProperty]
        private double outMin;

        [ObservableProperty]
        public double amend;

        [ObservableProperty]
        public string deviceNumber = string.Empty;

        [ObservableProperty]
        public string deviceName = string.Empty;

        [ObservableProperty]
        public string deviceAddress = string.Empty;

        #endregion

        public DevicePointViewModel(long number)
        {
            Dictionary<string, object> where = new Dictionary<string, object>();
            where["number"] = number.ToString();
            List<DevicePointModel> list = DevicePointModel.List(where);
            if (list.Count > 0)
            {
                this.model = list.First<DevicePointModel>();
                this.model.Adapt(this);
            }
            else
            {
                this.number = number;
            }
        }

        public void Apply()
        {
            this.Adapt(this.model);
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <returns></returns>
        public Response Add()
        {
            Response response = Response.DefaultResponse();
            int res = this.model.Add();
            if (res == 1)
            {
                response.Code = Response.SuccessCode;
                response.Msg = "添加成功";
            }
            else
            {
                response.Msg = "添加失败";
            }
            return response;
        }

        public Response Delete()
        {
            Response response = Response.DefaultResponse();
            int res = this.model.Delete();
            if (res == 1)
            {
                response.Code = Response.SuccessCode;
                response.Msg = "删除成功";
            }
            else
            {
                response.Msg = "删除失败";
            }
            return response;
        }

        /// <summary>
        /// 编辑
        /// </summary>
        /// <returns></returns>
        public Response Update()
        {
            Response response = Response.DefaultResponse();
            int res = this.model.Update();
            if (res == 1)
            {
                response.Code = Response.SuccessCode;
                response.Msg = "编辑成功";
            }
            else
            {
                response.Msg = "编辑失败";
            }
            return response;
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public List<DeviceModel> List(Dictionary<string, object> where)
        {
            return SqlSugarHelper.Inquireable<DeviceModel>(where);
        }
    }
}
